package com.glory.brepda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.EDCListBean;
import com.glory.brepda.entity.ScrapEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class EDCListAdapter extends BaseQuickAdapter<EDCListBean.ListBean, BaseViewHolder> {

   public EDCListAdapter(@Nullable List<EDCListBean.ListBean> data) {
      super(R.layout.item_edc_list,data);
   }

   @Override
   protected void convert(BaseViewHolder helper, EDCListBean.ListBean item) {
      helper.setText(R.id.tvName,item.getName());
      helper.setText(R.id.tvSubName,item.getSubName());
      helper.setText(R.id.etValue,item.getValue());
      EditText etValue = helper.getView(R.id.etValue);
      setEditListener(etValue,item);
   }

   private void setEditListener(EditText editText, EDCListBean.ListBean item) {
      if (editText.getTag() instanceof TextWatcher) {
         editText.removeTextChangedListener((TextWatcher) editText.getTag());
      }

      TextWatcher watcher = new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {

         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {

         }

         @Override
         public void afterTextChanged(Editable s) {
            try {
               item.setValue(s.toString());
               int i;
               if (s.toString().equals("")){
                  i = Integer.parseInt(s.toString());
               }else {
                  i = 0;
               }
               if(i<0) {
                  editText.setText(0+"");
               }else{
                  item.setValue(s.toString());
               }
            } catch (NumberFormatException e) {
               e.printStackTrace();
            }
         }
      };

      editText.addTextChangedListener(watcher);
      editText.setTag(watcher);
   }
}
